﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class frmLoanDetail2 : Form
    {
        private static CustomerItem cusItem;
        //private static EmployeeItem empItem;
        private static BondsmanItem bondsmanItem;
        public frmLoanDetail2()
        {
            InitializeComponent();
        }

        public frmLoanDetail2(CustomerItem item)
        {
            InitializeComponent();
            cusItem = item;
            bondsmanItem = DAL.BondsmanInfoDAL.LoadBondsmanByCusID(cusItem.CUSTOMER_ID);
            if (bondsmanItem == null)
            {
                this.btnBondsman.Visible = false;
            }
            this.txtAddress.Text = cusItem.CUSTOMER_ADDRESS;
            this.txtAddressWork.Text = cusItem.CUSTOMER_WORK_ADDRESS;
            this.txtAge.Text = cusItem.CUSTOMER_AGE.ToString();
            this.txtBlackList.Text = cusItem.CUSTOMER_BLACK_LIST.ToString();
            this.txtJob.Text = cusItem.CUSTOMER_JOB;
            this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;
            this.txtName.Text = cusItem.CUSTOMER_NAME;
            this.txtPresonalId.Text = cusItem.CUSTOMER_PERSONAL_ID;
            this.txtTel.Text = cusItem.CUSTOMER_TEL;
            this.txtTelWork.Text = cusItem.CUSTOMER_WORK_TEL;
            this.txtUserName.Text = cusItem.USERNAME;
            this.txtSalary.Text = cusItem.CUSTOMER_SARARY.ToString();
            genGridviewDept();
            genGridviewDoc();
            this.grdView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            this.grvDoc.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentDocClick);
        }

        //public frmLoanDetail2(CustomerItem item,EmployeeItem empItem2)
        //{
        //    InitializeComponent();
        //    cusItem = item;
        //    empItem = empItem2;
        //    this.txtAddress.Text = cusItem.CUSTOMER_ADDRESS;
        //    this.txtAddressWork.Text = cusItem.CUSTOMER_WORK_ADDRESS;
        //    this.txtAge.Text = cusItem.CUSTOMER_AGE.ToString();
        //    this.txtBlackList.Text = cusItem.CUSTOMER_BLACK_LIST.ToString();
        //    this.txtJob.Text = cusItem.CUSTOMER_JOB;
        //    this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;
        //    this.txtName.Text = cusItem.CUSTOMER_NAME;
        //    this.txtPresonalId.Text = cusItem.CUSTOMER_PERSONAL_ID;
        //    this.txtTel.Text = cusItem.CUSTOMER_TEL;
        //    this.txtTelWork.Text = cusItem.CUSTOMER_WORK_TEL;
        //    this.txtUserName.Text = cusItem.USERNAME;
        //    this.txtSalary.Text = cusItem.CUSTOMER_SARARY.ToString();
        //    genGridviewDept();
        //    genGridviewDoc();
        //    this.grdView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
        //    this.grvDoc.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentDocClick);

        //    if(empItem.EMPLOYEE_POSITION.Equals("h"))
        //    {
        //        this.btnAddDept.Visible = false;
        //        this.grdView.Enabled = false;
        //    }
        //}

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmCusDept frmCusDept = new frmCusDept(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()),"Edit");
                frmCusDept.ShowDialog();
                this.grdView.DataSource = LoadDeptCustomer();
            }
        }

        private void CellContentDocClick(object sender, DataGridViewCellEventArgs e)
        {
            CustomerDocItem item = DAL.CustomerDocInfoDAL.LoadCusDoc(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()));
            var tempFolder = System.IO.Path.GetTempPath();
            string filename = System.IO.Path.Combine(tempFolder, item.CUSTOMER_DOC_NAME + item.CUSTOMER_DOC_EXTENSION);
            System.IO.File.WriteAllBytes(filename, item.CUSTOMER_DOC_FILE);
            System.Diagnostics.Process.Start(filename);
        }

       
        private void btnAddDept_Click(object sender, EventArgs e)
        {
            frmCusDept frmCusDept = new frmCusDept(cusItem.CUSTOMER_ID,"Add");
            frmCusDept.ShowDialog();
            this.grdView.DataSource = LoadDeptCustomer();
        }

        private void genGridviewDept()
        {
            // gen Grideview
            grdView.AutoGenerateColumns = false;
            //Set Columns Count
            grdView.ColumnCount = 5;
            grdView.Columns[0].Width = 50;
            //Add Columns
            grdView.Columns[1].Name = "CUSTOMER_DEBT_ID";
            grdView.Columns[1].HeaderText = "ลำดับ";
            grdView.Columns[1].DataPropertyName = "CUSTOMER_DEBT_ID";
            grdView.Columns[1].Width = 35;

            grdView.Columns[2].Name = "DEBT_AMOUNT";
            grdView.Columns[2].HeaderText = "จำนวนเงิน";
            grdView.Columns[2].DataPropertyName = "DEBT_AMOUNT";
            grdView.Columns[2].Width = 100;

            grdView.Columns[3].Name = "DEBT_DETAIL";
            grdView.Columns[3].HeaderText = "รายละเอียด";
            grdView.Columns[3].DataPropertyName = "DEBT_DETAIL";
            grdView.Columns[3].Width = 250;

            grdView.Columns[4].Name = "CUSTOMER_DEBT_STATUS";
            grdView.Columns[4].HeaderText = "สถานะ";
            grdView.Columns[4].DataPropertyName = "CUSTOMER_DEBT_STATUS";
            grdView.Columns[4].Width = 65;
            // end gen gridview

            this.grdView.DataSource = LoadDeptCustomer();
        }

        private void genGridviewDoc()
        {
            // gen Grideview
            grvDoc.AutoGenerateColumns = false;
            //Set Columns Count
            grvDoc.ColumnCount = 3;
            grvDoc.Columns[0].Width = 50;
            //Add Columns
            grvDoc.Columns[1].Name = "CUSTOMER_DOC_ID";
            grvDoc.Columns[1].HeaderText = "รหัสเอกสาร";
            grvDoc.Columns[1].DataPropertyName = "CUSTOMER_DOC_ID";
            grvDoc.Columns[1].Width = 70;

            grvDoc.Columns[2].Name = "CUSTOMER_DOC_NAME";
            grvDoc.Columns[2].HeaderText = "ชื่อเอกสาร";
            grvDoc.Columns[2].DataPropertyName = "CUSTOMER_DOC_NAME";
            grvDoc.Columns[2].Width = 350;

            this.grvDoc.DataSource = LoadDocCustomer();
        }

        private DataTable LoadDeptCustomer() 
        {
            DataTable dt = CustomerDebtInfoDAL.LoadCusDeptAll(cusItem.CUSTOMER_ID);
            return dt;
        }

        private DataTable LoadDocCustomer()
        {
            DataTable dt = CustomerDocInfoDAL.LoadCusDocAll(cusItem.CUSTOMER_ID);
            return dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int salary = 0;
                bool result = Int32.TryParse(this.txtSalary.Text, out salary);
                if (result)
                {
                    cusItem.CUSTOMER_SARARY = Convert.ToInt32(this.txtSalary.Text);
                }
                else
                {
                    cusItem.CUSTOMER_SARARY = salary;
                }
                CustomerInfoDAL.Update(cusItem);
                MessageBox.Show("บันทึกข้อมูลสำเร็จ", "บันทึกข้อมูลลูกค้า");
            }
            catch (Exception ex) 
            {
                MessageBox.Show("ex : " + ex.Message, "บันทึกผิดพลาด");
            }
        }

        private void btnBondsman_Click(object sender, EventArgs e)
        {
            frmLoanDetail3 frmLoanDetail3 = new frmLoanDetail3(cusItem);
            frmLoanDetail3.ShowDialog();
        }
    }
}
